package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.service.UserCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/24 12:09
 */
@RestController
@RequestMapping("/selectingCourse")
public class SelectingCourseController {
    @Autowired
    private UserCourseService userCourseService;

    /*
    选课，发送个人课程信息，课程人数+1
     */
    @PostMapping("/{userId}/{courseId}/{courseCampIf}")
    public Result selectingCourse(@PathVariable Integer userId,
                                  @PathVariable Integer courseId,
                                  @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf != 0) {
            result.setCode(500);
            result.setMsg("训练营不属于当前服务");
            return result;
        }

        if (userCourseService.join(userId, courseId, courseCampIf)) {
            result.setCode(200);
            result.setMsg("选课成功,请准时参加");
        } else {
            result.setCode(500);
            result.setMsg("选课失败,课程余量不足");
        }

        return result;
    }

    @GetMapping("/{userId}/{courseId}/{courseCampIf}")
    public Result getCourseInfo(@PathVariable Integer userId,
                                @PathVariable Integer courseId,
                                @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf != 0) {
            result.setCode(500);
            result.setMsg("训练营不属于当前服务");
            return result;
        }
        if (userCourseService.getCourseQuantity(courseId) == 0) {
            result.setCode(500);
            result.setMsg("课余量不足");
            return result;
        } else if (userCourseService.latestTimeLimit(courseId)) {
            result.setCode(500);
            result.setMsg("开课前五分钟不能选课");
            return result;
        } else if (userCourseService.getCourseInfo(userId, courseId, courseCampIf)) {
            result.setCode(500);
            result.setMsg("已选择该课或者课程冲突");
            return result;
        } else {
            result.setCode(200);
            result.setMsg("可以选课");
        }
        return result;
    }
    /*获取课余量

     */
    @GetMapping("/{courseId}/{courseCampIf}")
    public Result getCourseQuantity(@PathVariable Integer courseId,
                                @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf != 0) {
            result.setCode(500);
            result.setMsg("训练营不属于当前服务");
            return result;
        }
        Integer courseQuantity = userCourseService.getCourseQuantity(courseId);
        if (courseQuantity>0) {
            result.setCode(200);
            result.setMsg("课余量还有");
            result.setData(courseQuantity);
        } else {
            result.setCode(500);
            result.setMsg("课余量已满");
        }
        return result;
    }


    /*
    退课,发送个人课程信息申请,针对非训练营课程
     */
    @DeleteMapping("/{courseId}/{courseCampIf}")
    public Result whetherApply(@PathVariable Integer courseId,
                               @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf != 0) {
            result.setCode(500);
            result.setMsg("训练营不属于当前服务");
            return result;
        }

        if (userCourseService.whetherApply(courseId)) {
            result.setCode(200);
            result.setMsg("允许申请退课");
        } else {
            result.setCode(500);
            result.setMsg("不允许申请退课");
        }
        return result;
    }

    @DeleteMapping("/{userId}/{courseId}/{courseCampIf}")
    public Result cancelCourse(@PathVariable Integer userId,
                               @PathVariable Integer courseId,
                               @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf != 0) {
            result.setCode(500);
            result.setMsg("训练营不属于当前服务");
            return result;
        }
            if (userCourseService.dropCourse(userId, courseId, courseCampIf)) {
                result.setCode(200);
                result.setMsg("退课成功,祝你生活愉快");
            }
         else {
            result.setCode(500);
            result.setMsg("退课失败,当前时间不允许申请退课");
        }
        return result;
    }
    @GetMapping("/{userId}")
    public Result getPersonCourse(@PathVariable Integer userId){
        Result result=new Result();
        result.setCode(200);
        return result;
    }
}
